var ns, toast, actionsheet, WXPhotoPicker;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            ID: 0,
            DeductCount:0,
            CompleteDesc: '',
            action: 'completecheckpoint',
        },
        writeCount:0,//输入的文字数量
        IsChangeBt:false,
        CheckProjectScore:0,//项目分值
        DeductScore:0,//项目扣除分值
        KouScore:0,//扣除分值
        newform:{},
        imglist: [],
        maxuploadcount: CONFIG.maxuploadcount,

    },
    methods: {
        do_save: function() {
            var that = this;
            if(that.KouScore>0){
                api.confirm({
                    title: '提示',
                    msg: '本次考核存在扣分项目，是否完成考核，并创建整改任务？',
                    buttons: ['直接完成','创建任务']
                }, function(ret, err) {
                    if (ret) {
                        if (ret.buttonIndex == 1) {
                            that.do_sureSave(2);
                        }else{
                            that.do_zhenggai();
                        }
                    }
                })
                        
            }else{
                that.do_sureSave(2);
            }
        },
        //整改工单
        do_zhenggai:function(){
            var that=this;
            that.do_sureSave(1);
            // setTimeout(function() {
                // ns.openWin('qualitycontrol_frm','新增整改工单',{
                //     RelatedID:that.form.ID,
                //     ComeFrom:2
                // });
            // }, 500)
            
        },
        //保存
         do_sureSave: function(type) {
             var that=this;
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                filelist.push(that.imglist[i].url);
            }
            ns.post(that.form, function(succeed, data, err) { 
                if (succeed) {
                    ns.toast('保存成功');
                    api.sendEvent({
                        name: 'customerservicelist_frm',
                    });
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                    api.sendEvent({
                        name: 'updata_task_con',
                        extra: {
                            key1: 'value1'
                        }
                    });
                    if(type==1){
                        api.addEventListener({
                            name: 'viewdisappear'
                        }, function (ret, err) {
                            api.closeWin();
                        });
                        ns.openWin('qualitycontrol_frm','新增整改工单',{
                            RelatedID:that.form.ID,
                            ComeFrom:2
                        });
                    }
                   
                    setTimeout(function() {
                       api.closeWin(); 
                    }, 200);
                    
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                files: {
                    file: filelist
                },
                toast: true,
                toastmsg: '提交中'
            });
        },
        choose_picture: function() {
            var that = this;
            ns.confirmPer('camera', function() {
                that.choose_picture_start();
            })
        },
        choose_picture_start:function(){
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.actionSheet({
                cancelTitle: '取消',
                buttons: ['拍照', '从相册选择']
            }, function(ret, err) {
                //拍照
                if(ret.buttonIndex==1){ 
                    that.upload_choose_picture();
                    return;
                }
                //从相册选择
                if(ret.buttonIndex==2){
                    that.choose_picture_album();
                    return;
                } 
                //取消 
                else{
                    return false;  
                }  
            });
        },
        upload_choose_picture:function(){
            var that = this;
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'jpg',
                destinationType: 'url',
                allowEdit: false,
                saveToPhotoAlbum: true,
            }, function(ret, err) {
                if (ret&&ret.data) {
                    that.imglist.push({
                        ID: 0,
                        url: ret.data,
                        thumbPath: ret.data,
                        isupload: true
                    });
                } 
            });
        },
        choose_picture_album:function(){
            var that = this;
            WXPhotoPicker.open({
                max: that.maxuploadcount - that.imglist.length,
                type:'image',
            }, function(ret) {
                if (ret) {
                    if (ret.list.length > 0) {
                        ret.list.forEach((item) => {
                            that.imglist.push({
                                ID: 0,
                                url: item.path,
                                thumbPath: item.path,
                                isupload: true
                            });
                        });
                    }
                }
            });
        },
        remove_img: function (index,item) {
            var that = this;
            that.imglist.splice(index, 1);
        },
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
        do_reduce_month: function (pointscore) {
            var that = this;
            if(pointscore==0){
                api.toast({
                    msg: '检查点分值为零，不能扣分',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if(that.form.DeductCount==0){
                api.toast({
                    msg: '已是最小值，不能再减了',
                    duration: 2000,
                    location: 'bottom'
                });
            }else{
                that.form.DeductCount--;
                that.KouScore= that.form.DeductCount * pointscore;
            }
        },

        writeMsg:function(){
            var that=this;
            var testVal=that.form.CompleteDesc.length;
            that.writeCount=testVal;
        },

        do_add_month: function (pointscore) {
            var that = this;
            if(pointscore==0){
                api.toast({
                    msg: '检查点分值为零，不能扣分',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            that.form.DeductCount++;
            var maxnum=that.form.DeductCount*pointscore;
            if(maxnum>(that.CheckProjectScore-that.DeductScore)){
                api.confirm({
                    title: '提示',
                    msg: '最大扣分值不能大于检查项目分值',
                    buttons: ['确定','取消']
                }, function(ret, err) {
                    if (ret) {
                        if (ret.buttonIndex == 1) {
                            // that.form.DeductCount=that.CheckProjectScore/pointscore;
                            that.form.DeductCount=Math.ceil((that.CheckProjectScore-that.DeductScore)/pointscore);
                            that.KouScore=that.CheckProjectScore-that.DeductScore;

                        }else{
                            that.form.DeductCount=1;
                            that.KouScore=that.form.DeductCount*pointscore;
                        }
                    }
                });
                
            }else{
                that.KouScore=that.form.DeductCount*pointscore
            }
        },
    }
});

apiready = function() {
    ns = window.Foresight.Util;
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    WXPhotoPicker = api.require('WXPhotoPicker');
    app.CheckProjectScore = api.pageParam.CheckProjectScore;
    app.DeductScore = api.pageParam.DeductScore;
    app.newform = api.pageParam.item;
    app.form.ID=api.pageParam.item.ID;
    app.IsChangeBt=api.pageParam.IsChangeBt;
    // app.KouScore=api.pageParam.item.CheckPointScore;
    api.addEventListener({
        name: 'saveservicelist'
    }, function(ret, err) {
        // setTimeout(function() {
            api.closeWin();
            // app.do_sureSave();
        // }, 500);
    });

}
